package net.lahuobao.mall.service.impl.admin;

import net.lahuobao.enums.mall.admin.EMallSubsidySource;
import net.lahuobao.enums.mall.admin.EMallSubsidyType;
import net.lahuobao.mall.entity.admin.MallSubsidyDetailEntity;
import net.lahuobao.mall.entity.admin.MallSubsidyEntity;
import net.lahuobao.mall.entity.admin.resultmodel.MallSubsidyResultEntity;
import net.lahuobao.mall.service.admin.MallSubsidyDetailServiceI;
import net.lahuobao.mall.service.admin.MallSubsidyServiceI;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.jeecgframework.core.util.MyBeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service("mallSubsidyService")
@Transactional
public class MallSubsidyServiceImpl extends CommonServiceImpl implements MallSubsidyServiceI {

    @Autowired
    private MallSubsidyDetailServiceI detailService;

    /**
     * 保存折扣信息
     * @param subsidyEntity
     * @param mallSubsidyDetailEntityList
     */
    @Override
    public void save(MallSubsidyEntity subsidyEntity, List<MallSubsidyDetailEntity> mallSubsidyDetailEntityList) {
        subsidyEntity.setSubsidysource(EMallSubsidySource.Platform.getValue());
        super.save(subsidyEntity);
        saveSubsidyDetail(subsidyEntity, mallSubsidyDetailEntityList);
    }

    /**
     * 更新折扣信息
     * @param subsidyEntity
     * @param mallSubsidyDetailEntityList
     */
    @Override
    public void update(MallSubsidyEntity subsidyEntity, List<MallSubsidyDetailEntity> mallSubsidyDetailEntityList) {
        //更新主表
        super.saveOrUpdate(subsidyEntity);
        //先删除子表的记录在保存子表的记录
        String sql = "delete from  lahuobao.mall_subsidy_detail where subsidyId = ? ";
        super.executeSql(sql, subsidyEntity.getId());
        saveSubsidyDetail(subsidyEntity, mallSubsidyDetailEntityList);

    }

    /**
     * 保存折扣明细表信息
      * @param subsidyEntity
     * @param mallSubsidyDetailEntityList
     */
   private void saveSubsidyDetail(MallSubsidyEntity subsidyEntity, List<MallSubsidyDetailEntity> mallSubsidyDetailEntityList){
       for(MallSubsidyDetailEntity entity : mallSubsidyDetailEntityList){
           entity.setSubsidyid(subsidyEntity.getId());
           if(subsidyEntity.getSubsidytype() != EMallSubsidyType.Mj.getValue()){//不是满减
               //设置最大最小金额
               entity.setMinmoney(new BigDecimal(0));
               entity.setMaxmoney(new BigDecimal(0));
           }
           detailService.save(entity);
       }
   }

    /**
     * 根据商品id获取商品折扣信息
     * @param productId
     * @return
     */
    @Override
    public MallSubsidyResultEntity selectSubsidyByProductId(Integer productId){
        //获取折扣信息
        MallSubsidyEntity subsidyEntity = super.findUniqueByProperty(MallSubsidyEntity.class, "productid", productId);
        //设置返回信息
        MallSubsidyResultEntity resultEntity = new MallSubsidyResultEntity();
        if(null != subsidyEntity){
            //获取折扣详情
            List<MallSubsidyDetailEntity> subsidyDetailEntities =
                    detailService.findByProperty(MallSubsidyDetailEntity.class, "subsidyid", subsidyEntity.getId());
            try{
                MyBeanUtils.copyBean2Bean(resultEntity, subsidyEntity);
            }catch (Exception e){
                e.printStackTrace();
            }
            resultEntity.setSubsidyDetailEntities(subsidyDetailEntities);
        }
        return resultEntity;
    }
}